from time import sleep
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from time import sleep
class Login:
    def __init__(self): # 构造方法，类实例化的时候就执行
        self.driver = webdriver.Chrome()
        self.driver.get('http://localhost/ranzhi/www/sys/user-login.html')
        self.driver.implicitly_wait(5)
    def login(self):
        self.driver.find_element_by_xpath('//*[@id="account"]').send_keys('admin')
        self.driver.find_element_by_xpath('//*[@id="password"]').send_keys('111111')
        self.driver.find_element_by_xpath('//*[@id="submit"]').click()
    def dingwei(self):
        # time.sleep(2)
        self.driver.find_element_by_id('s-menu-superadmin').click()

        frame = self.driver.find_element_by_xpath('//*[@id="iframe-superadmin"]')
        self.driver.switch_to_frame(frame)
        self.driver.find_element_by_link_text('添加成员').click()
    def addUser(self,name,relname,sex,deptname,role,passwd,rpasswd,email):#genderm 男  genderf 女  
        # sleep(2)
        self.driver.find_element_by_id("account").send_keys(name)
        self.driver.find_element_by_id("realname").send_keys(relname)
        self.driver.find_element_by_id("password1").send_keys(passwd)
        self.driver.find_element_by_id("password2").send_keys(rpasswd)
        self.driver.find_element_by_id(sex).click()
        select = self.driver.find_element_by_id('dept')
        select1 = self.driver.find_element_by_id('role')
        depts = Select(select)
        roles = Select(select1)
        depts.select_by_visible_text(deptname)
        roles.select_by_visible_text(role)

        self.driver.find_element_by_id("email").send_keys(email)
        self.driver.find_element(By.ID, "submit").click()
    def asserts(self,name):
        f = self.driver.find_elements_by_xpath('/html/body/div/div/div/div[2]/div/div/table/tbody/tr/td[2]')
        a = []
        for i in f:
            a.append(i.text)
        assert name in a,'添加失败'
if __name__ == '__main__':
    login = Login()
    try:    
        a = [
            ('zhangsan','张三','genderm','/酱油部门','市场','111111','111111','113231960@qq.com'),
            ('wangwu','王五','genderm','/酱油部门','市场','111111','111111','11318330@qq.com'),
            ('lisi','李四','genderm','/酱油部门','市场','111111','111111','113144360@qq.com'),
            ('zhaoliu','赵六','genderm','/酱油部门','市场','111111','111111','11231e8960@qq.com'),
            ('tianqi','天启','genderm','/酱油部门','市场','111111','111111','1131442e60@qq.com')       
             ]
        login.login()
        login.dingwei()
        for i in a:
            # login.addUser('wangwu','王五','genderm','/酱油部门','市场','111111','111111','11318960@qq.com')
           login.addUser(i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7])
           #做判断
           #定位元素 和name i[0]
           sleep(2)
           login.asserts(i[1])
           login.driver.find_element_by_link_text('添加成员').click()
           
    except Exception as e:
         print(e)
    finally:
        sleep(2)
        # login.driver.quit()